/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 * */

// AmzServer - Tototoy's server model.
// Author: tonyjobmails@gmail.com (tonyhack).
//
// Define class UDPPacketSocketCommunicator.
//
// UDPPacketSocketCommunicator uses to send/receive UDP packet.
// This UDP uses packet pattern, each send will packet a UDP packet.
//

#ifndef __UDP__PACKET__SOCKET__COMMUNICATOR__H
#define __UDP__PACKET__SOCKET__COMMUNICATOR__H

#include "udp_socket_communicator.h"
#include "ring_queue_single_model_boundless.h"

namespace amz {

class UDPPacketSocketCommunicator : public UDPSocketCommunicator {
  typedef std::pair<uint32, SimpleByteBufferDynamic> SendBufferAssist;
 public:
  UDPPacketSocketCommunicator(const sockaddr_in &address, int len,
         UDPSocket *udp_socket, CompressType compress, EncryptType encrypt,
         int flag = 0);
  virtual ~UDPPacketSocketCommunicator();

  // Send message to buffer.
  virtual int Send(const char *message, int size);
  // Receive from buffer.
  virtual int Receive(char *message, int max_size);

  // Send message to socket.
  virtual int Send();

  // After receiving any data, dispatch to the buffer(according to address).
  virtual int PushReceive(char *message, int size);

 protected:
  // Limit of packet size
  uint32 max_message_size_;
  // Buffers.
  RingQueueSingleModelBoundless send_buffer_;
  SendBufferAssist send_buffer_assist_;
  RingQueueSingleModelBoundless receive_buffer_;

  SimpleByteBufferDynamic buffer_;
};

}  // namespace amz

#endif  // __UDP__PACKET__SOCKET__COMMUNICATOR__H
